{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "<a href=\"https://colab.research.google.com/drive/1IVmnJ7dOh9PnCLjZ4THwKfDMaxiMtFTA?usp=sharing\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>"
      ],
      "metadata": {
        "id": "OzqtDffTDR-5"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Plan-and-Solve Prompting"
      ],
      "metadata": {
        "id": "-MHAQ9kLTQga"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "3qUCixlR6O8x"
      },
      "outputs": [],
      "source": [
        "!pip install -qU google-generativeai"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import google.generativeai as genai\n",
        "import getpass"
      ],
      "metadata": {
        "id": "nX8UGmaeTT1o"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Get free-tier Google's Gemini API Key here: https://aistudio.google.com/app/apikey"
      ],
      "metadata": {
        "id": "wo30gfCHTWAO"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "API_KEY = getpass.getpass(\"Enter your Google API key: \")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "guHWz5cBTYcx",
        "outputId": "a7957203-3603-42cb-e1ad-cece6b4c8273"
      },
      "execution_count": 7,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Enter your Google API key: ··········\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "genai.configure(api_key=API_KEY)"
      ],
      "metadata": {
        "id": "BYdEbX39TdfC"
      },
      "execution_count": 10,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "class PlanAndSolveAgent:\n",
        "    def __init__(self):\n",
        "        self.model = genai.GenerativeModel(\"gemini-2.0-flash\")\n",
        "\n",
        "    def basic_cot(self, problem):\n",
        "        \"\"\"Basic Chain-of-Thought (baseline for comparison)\"\"\"\n",
        "        print(f\"\\n{'='*60}\")\n",
        "        print(f\"📝 Basic CoT (Baseline)\")\n",
        "        print(f\"{'='*60}\")\n",
        "        print(f\"Problem: {problem}\\n\")\n",
        "\n",
        "        prompt = f\"\"\"{problem}\n",
        "\n",
        "        Let's think step by step.\"\"\"\n",
        "\n",
        "        response = self.model.generate_content(prompt).text\n",
        "        print(f\"Response:\\n{response}\\n\")\n",
        "\n",
        "        return response\n",
        "\n",
        "    def plan_and_solve(self, problem):\n",
        "        \"\"\"Plan-and-Solve: Enhanced structured reasoning\"\"\"\n",
        "        print(f\"\\n{'='*60}\")\n",
        "        print(f\"🎯 Plan-and-Solve\")\n",
        "        print(f\"{'='*60}\")\n",
        "        print(f\"Problem: {problem}\\n\")\n",
        "\n",
        "        # The key prompt engineering: explicit planning instruction\n",
        "        prompt = f\"\"\"{problem}\n",
        "\n",
        "        Let's first understand the problem and devise a plan to solve it. Then,\n",
        "\n",
        "        let's carry out the plan step by step, doing the calculations carefully.\"\"\"\n",
        "\n",
        "        response = self.model.generate_content(prompt).text\n",
        "        print(f\"Response:\\n{response}\\n\")\n",
        "\n",
        "        return response\n",
        "\n",
        "    def plan_and_solve_plus(self, problem):\n",
        "        \"\"\"Plan-and-Solve+: More detailed guidance\"\"\"\n",
        "        print(f\"\\n{'='*60}\")\n",
        "        print(f\"🚀 Plan-and-Solve+ (Enhanced)\")\n",
        "        print(f\"{'='*60}\")\n",
        "        print(f\"Problem: {problem}\\n\")\n",
        "\n",
        "        # Step 1: Extract relevant information\n",
        "        print(\"📋 Step 1: Extracting relevant information...\\n\")\n",
        "\n",
        "        extract_prompt = f\"\"\"{problem}\n",
        "\n",
        "        Extract the relevant variables and their values from this problem. List them clearly.\"\"\"\n",
        "\n",
        "        extraction = self.model.generate_content(extract_prompt).text\n",
        "        print(f\"Extracted Info:\\n{extraction}\\n\")\n",
        "\n",
        "        # Step 2: Generate high-level plan\n",
        "        print(\"🗺️  Step 2: Generating high-level plan...\\n\")\n",
        "\n",
        "        plan_prompt = f\"\"\"{problem}\n",
        "\n",
        "        Relevant information:\n",
        "        {extraction}\n",
        "\n",
        "        Create a numbered step-by-step plan to solve this problem. Be specific.\"\"\"\n",
        "\n",
        "        plan = self.model.generate_content(plan_prompt).text\n",
        "        print(f\"Plan:\\n{plan}\\n\")\n",
        "\n",
        "        # Step 3: Execute plan with careful calculations\n",
        "        print(\"⚡ Step 3: Executing plan with careful calculations...\\n\")\n",
        "\n",
        "        execute_prompt = f\"\"\"{problem}\n",
        "\n",
        "        Plan:\n",
        "        {plan}\n",
        "\n",
        "        Now execute this plan step by step. Show all calculations clearly and verify each step.\"\"\"\n",
        "\n",
        "        execution = self.model.generate_content(execute_prompt).text\n",
        "        print(f\"Execution:\\n{execution}\\n\")\n",
        "\n",
        "        return execution\n",
        "\n",
        "    def compare_methods(self, problem):\n",
        "        \"\"\"Compare all three approaches\"\"\"\n",
        "        print(f\"\\n{'='*70}\")\n",
        "        print(f\"🔬 COMPARING METHODS\")\n",
        "        print(f\"{'='*70}\\n\")\n",
        "\n",
        "        # Method 1: Basic CoT\n",
        "        basic_result = self.basic_cot(problem)\n",
        "\n",
        "        # Method 2: Plan-and-Solve\n",
        "        ps_result = self.plan_and_solve(problem)\n",
        "\n",
        "        # Method 3: Plan-and-Solve+\n",
        "        ps_plus_result = self.plan_and_solve_plus(problem)\n",
        "\n",
        "        print(f\"{'='*70}\")\n",
        "        print(f\"📊 SUMMARY\")\n",
        "        print(f\"{'='*70}\")\n",
        "        print(f\"All three methods completed. Review outputs above.\")\n",
        "        print()"
      ],
      "metadata": {
        "id": "bgEAPxktTfus"
      },
      "execution_count": 11,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Example 1: Math Word Problem\n",
        "print(\"=\"*60)\n",
        "print(\"EXAMPLE 1: Math Word Problem\")\n",
        "print(\"=\"*60)\n",
        "\n",
        "agent1 = PlanAndSolveAgent()\n",
        "problem1 = \"\"\"A group of 4 friends want to split a restaurant bill equally.\n",
        "The food costs $85, drinks cost $32, and they want to leave a 20% tip on the total.\n",
        "How much should each person pay?\"\"\"\n",
        "\n",
        "agent1.plan_and_solve(problem1)\n",
        "\n",
        "\n",
        "# Example 2: Multi-Step Calculation\n",
        "print(\"\\n\" + \"=\"*60)\n",
        "print(\"EXAMPLE 2: Multi-Step Calculation\")\n",
        "print(\"=\"*60)\n",
        "\n",
        "agent2 = PlanAndSolveAgent()\n",
        "problem2 = \"\"\"A store is having a sale. A laptop originally costs $800.\n",
        "It's on sale for 25% off, but there's an additional 10% off coupon that applies to the sale price.\n",
        "Then there's 8% sales tax on the final discounted price. What's the total price?\"\"\"\n",
        "\n",
        "agent2.plan_and_solve(problem2)\n",
        "\n",
        "\n",
        "# Example 3: Logic Problem\n",
        "print(\"\\n\" + \"=\"*60)\n",
        "print(\"EXAMPLE 3: Logic Reasoning\")\n",
        "print(\"=\"*60)\n",
        "\n",
        "agent3 = PlanAndSolveAgent()\n",
        "problem3 = \"\"\"In a race, Amy finished before Bob.\n",
        "Carol finished after Bob but before Diana.\n",
        "Emma finished before Amy.\n",
        "What is the order of finishers from first to last?\"\"\"\n",
        "\n",
        "agent3.plan_and_solve(problem3)\n",
        "\n",
        "\n",
        "# Example 4: Complex Word Problem\n",
        "print(\"\\n\" + \"=\"*60)\n",
        "print(\"EXAMPLE 4: Complex Word Problem\")\n",
        "print(\"=\"*60)\n",
        "\n",
        "agent4 = PlanAndSolveAgent()\n",
        "problem4 = \"\"\"A train travels from City A to City B at 60 mph.\n",
        "Another train travels from City B to City A at 80 mph.\n",
        "The cities are 420 miles apart.\n",
        "If both trains leave at the same time, how long until they meet?\"\"\"\n",
        "\n",
        "agent4.plan_and_solve_plus(problem4)\n",
        "\n",
        "\n",
        "# Example 5: Puzzle Solving\n",
        "print(\"\\n\" + \"=\"*60)\n",
        "print(\"EXAMPLE 5: Number Puzzle\")\n",
        "print(\"=\"*60)\n",
        "\n",
        "agent5 = PlanAndSolveAgent()\n",
        "problem5 = \"\"\"I'm thinking of a number.\n",
        "If you double it and add 7, you get 31.\n",
        "What's the number?\"\"\"\n",
        "\n",
        "agent5.plan_and_solve(problem5)\n",
        "\n",
        "\n",
        "# Example 6: Planning Problem\n",
        "print(\"\\n\" + \"=\"*60)\n",
        "print(\"EXAMPLE 6: Project Planning\")\n",
        "print(\"=\"*60)\n",
        "\n",
        "agent6 = PlanAndSolveAgent()\n",
        "problem6 = \"\"\"You need to complete a project in 10 days.\n",
        "Task A takes 3 days, Task B takes 4 days, Task C takes 2 days.\n",
        "Task B depends on Task A being complete. Task C can run parallel to anything.\n",
        "Can you finish in time? What's the optimal schedule?\"\"\"\n",
        "\n",
        "agent6.plan_and_solve_plus(problem6)\n",
        "\n",
        "\n",
        "# Example 7: Compare All Methods\n",
        "print(\"\\n\" + \"=\"*60)\n",
        "print(\"EXAMPLE 7: Method Comparison\")\n",
        "print(\"=\"*60)\n",
        "\n",
        "agent7 = PlanAndSolveAgent()\n",
        "problem7 = \"\"\"Sarah has 3 boxes of cookies. Each box has 12 cookies.\n",
        "She gives 8 cookies to her friend and eats 5 herself.\n",
        "How many cookies does she have left?\"\"\"\n",
        "\n",
        "agent7.compare_methods(problem7)\n",
        "\n",
        "\n",
        "# Example 8: Algorithm Design\n",
        "print(\"\\n\" + \"=\"*60)\n",
        "print(\"EXAMPLE 8: Algorithm Design\")\n",
        "print(\"=\"*60)\n",
        "\n",
        "agent8 = PlanAndSolveAgent()\n",
        "problem8 = \"\"\"Design an algorithm to check if a string is a palindrome.\n",
        "What are the steps? What edge cases should be considered?\"\"\"\n",
        "\n",
        "agent8.plan_and_solve(problem8)\n",
        "\n",
        "\n",
        "print(\"✅ Plan-and-Solve Complete!\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "r3vvbv-NTtH_",
        "outputId": "c20b7224-f405-4caa-c497-21e2711e9866"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "============================================================\n",
            "EXAMPLE 1: Math Word Problem\n",
            "============================================================\n",
            "\n",
            "============================================================\n",
            "🎯 Plan-and-Solve\n",
            "============================================================\n",
            "Problem: A group of 4 friends want to split a restaurant bill equally. \n",
            "The food costs $85, drinks cost $32, and they want to leave a 20% tip on the total. \n",
            "How much should each person pay?\n",
            "\n",
            "Response:\n",
            "**Plan:**\n",
            "\n",
            "1.  **Calculate the total cost of the meal (food + drinks).**\n",
            "2.  **Calculate the tip amount (20% of the total cost).**\n",
            "3.  **Calculate the total bill (total cost + tip).**\n",
            "4.  **Divide the total bill by the number of friends to find out how much each person should pay.**\n",
            "\n",
            "**Calculations:**\n",
            "\n",
            "1.  **Total cost of the meal:**\n",
            "    \\$85 (food) + \\$32 (drinks) = \\$117\n",
            "2.  **Tip amount:**\n",
            "    \\$117 * 0.20 = \\$23.40\n",
            "3.  **Total bill:**\n",
            "    \\$117 + \\$23.40 = \\$140.40\n",
            "4.  **Cost per person:**\n",
            "    \\$140.40 / 4 = \\$35.10\n",
            "\n",
            "**Answer:**\n",
            "\n",
            "Each person should pay \\$35.10.\n",
            "\n",
            "\n",
            "\n",
            "============================================================\n",
            "EXAMPLE 2: Multi-Step Calculation\n",
            "============================================================\n",
            "\n",
            "============================================================\n",
            "🎯 Plan-and-Solve\n",
            "============================================================\n",
            "Problem: A store is having a sale. A laptop originally costs $800. \n",
            "It's on sale for 25% off, but there's an additional 10% off coupon that applies to the sale price. \n",
            "Then there's 8% sales tax on the final discounted price. What's the total price?\n",
            "\n",
            "Response:\n",
            "**Plan:**\n",
            "\n",
            "1.  **Calculate the initial discount:** Find 25% of the original price.\n",
            "2.  **Calculate the sale price:** Subtract the initial discount from the original price.\n",
            "3.  **Calculate the coupon discount:** Find 10% of the sale price.\n",
            "4.  **Calculate the price after the coupon:** Subtract the coupon discount from the sale price.\n",
            "5.  **Calculate the sales tax:** Find 8% of the price after the coupon.\n",
            "6.  **Calculate the total price:** Add the sales tax to the price after the coupon.\n",
            "\n",
            "**Step-by-step calculations:**\n",
            "\n",
            "1.  **Initial Discount:**\n",
            "    *   25% of $800 = 0.25 * $800 = $200\n",
            "\n",
            "2.  **Sale Price:**\n",
            "    *   $800 - $200 = $600\n",
            "\n",
            "3.  **Coupon Discount:**\n",
            "    *   10% of $600 = 0.10 * $600 = $60\n",
            "\n",
            "4.  **Price After Coupon:**\n",
            "    *   $600 - $60 = $540\n",
            "\n",
            "5.  **Sales Tax:**\n",
            "    *   8% of $540 = 0.08 * $540 = $43.20\n",
            "\n",
            "6.  **Total Price:**\n",
            "    *   $540 + $43.20 = $583.20\n",
            "\n",
            "**Answer:** The total price of the laptop is $583.20.\n",
            "\n",
            "\n",
            "\n",
            "============================================================\n",
            "EXAMPLE 3: Logic Reasoning\n",
            "============================================================\n",
            "\n",
            "============================================================\n",
            "🎯 Plan-and-Solve\n",
            "============================================================\n",
            "Problem: In a race, Amy finished before Bob. \n",
            "Carol finished after Bob but before Diana. \n",
            "Emma finished before Amy. \n",
            "What is the order of finishers from first to last?\n",
            "\n",
            "Response:\n",
            "**Understanding the Problem**\n",
            "\n",
            "We're given a series of relative finishing positions in a race and need to determine the absolute finishing order from first to last.\n",
            "\n",
            "**Plan**\n",
            "\n",
            "1.  Represent the known relationships using inequalities or symbols.\n",
            "2.  Combine the relationships to deduce the complete order.\n",
            "\n",
            "**Step-by-Step Solution**\n",
            "\n",
            "1.  **Representing the relationships:**\n",
            "    *   Amy finished before Bob: Amy < Bob (where \"<\" means \"finished before\")\n",
            "    *   Carol finished after Bob but before Diana: Bob < Carol < Diana\n",
            "    *   Emma finished before Amy: Emma < Amy\n",
            "\n",
            "2.  **Combining the relationships:**\n",
            "\n",
            "    *   We have: Emma < Amy < Bob\n",
            "    *   And: Bob < Carol < Diana\n",
            "\n",
            "    *   Combining these: Emma < Amy < Bob < Carol < Diana\n",
            "\n",
            "**Final Answer:**\n",
            "\n",
            "The order of finishers from first to last is: Emma, Amy, Bob, Carol, Diana.\n",
            "\n",
            "\n",
            "\n",
            "============================================================\n",
            "EXAMPLE 4: Complex Word Problem\n",
            "============================================================\n",
            "\n",
            "============================================================\n",
            "🚀 Plan-and-Solve+ (Enhanced)\n",
            "============================================================\n",
            "Problem: A train travels from City A to City B at 60 mph. \n",
            "Another train travels from City B to City A at 80 mph. \n",
            "The cities are 420 miles apart. \n",
            "If both trains leave at the same time, how long until they meet?\n",
            "\n",
            "📋 Step 1: Extracting relevant information...\n",
            "\n",
            "Extracted Info:\n",
            "Here's a breakdown of the relevant variables and their values:\n",
            "\n",
            "*   **Distance between City A and City B:** 420 miles\n",
            "*   **Speed of train from City A to City B:** 60 mph\n",
            "*   **Speed of train from City B to City A:** 80 mph\n",
            "\n",
            "\n",
            "🗺️  Step 2: Generating high-level plan...\n",
            "\n",
            "Plan:\n",
            "Here's a step-by-step plan to solve this problem:\n",
            "\n",
            "1.  **Calculate the combined speed:**  Since the trains are traveling towards each other, their speeds add up.  Add the speed of the train from City A (60 mph) to the speed of the train from City B (80 mph).  Combined speed = 60 mph + 80 mph = 140 mph.\n",
            "\n",
            "2.  **Apply the formula: Time = Distance / Speed:** Use the formula to calculate the time it takes for the trains to meet. The distance is the total distance between the cities (420 miles), and the speed is the combined speed calculated in Step 1 (140 mph).  Time = 420 miles / 140 mph.\n",
            "\n",
            "3.  **Calculate the time:** Perform the division to find the time in hours.\n",
            "\n",
            "4.  **State the answer:**  Write down the time it takes for the trains to meet.\n",
            "\n",
            "\n",
            "⚡ Step 3: Executing plan with careful calculations...\n",
            "\n",
            "Execution:\n",
            "Okay, let's execute the plan step by step.\n",
            "\n",
            "**Step 1: Calculate the combined speed**\n",
            "\n",
            "*   Train from City A speed: 60 mph\n",
            "*   Train from City B speed: 80 mph\n",
            "*   Combined speed = 60 mph + 80 mph = 140 mph\n",
            "\n",
            "*Verification:* Simple addition.  Seems correct.\n",
            "\n",
            "**Step 2: Apply the formula: Time = Distance / Speed**\n",
            "\n",
            "*   Distance: 420 miles\n",
            "*   Combined Speed: 140 mph\n",
            "*   Time = Distance / Speed = 420 miles / 140 mph\n",
            "\n",
            "*Verification:* The formula is correctly stated and the values are plugged in correctly.\n",
            "\n",
            "**Step 3: Calculate the time**\n",
            "\n",
            "*   Time = 420 miles / 140 mph = 3 hours\n",
            "\n",
            "*Verification:*  420 / 140 = 3. This calculation is correct.\n",
            "\n",
            "**Step 4: State the answer**\n",
            "\n",
            "The trains will meet in 3 hours.\n",
            "\n",
            "**Final Answer:** The trains will meet in 3 hours.\n",
            "\n",
            "\n",
            "\n",
            "============================================================\n",
            "EXAMPLE 5: Number Puzzle\n",
            "============================================================\n",
            "\n",
            "============================================================\n",
            "🎯 Plan-and-Solve\n",
            "============================================================\n",
            "Problem: I'm thinking of a number. \n",
            "If you double it and add 7, you get 31. \n",
            "What's the number?\n",
            "\n",
            "Response:\n",
            "**Plan:**\n",
            "\n",
            "1.  **Represent the unknown:** Let's use the variable \"x\" to represent the number I'm thinking of.\n",
            "2.  **Write the equation:** Translate the problem's description into a mathematical equation.\n",
            "3.  **Solve the equation:** Use algebraic manipulation to isolate \"x\" and find its value.\n",
            "\n",
            "**Execution:**\n",
            "\n",
            "1.  **Represent the unknown:**\n",
            "    *   Let x = the number I'm thinking of.\n",
            "\n",
            "2.  **Write the equation:**\n",
            "    *   \"Double it\": 2x\n",
            "    *   \"Add 7\": 2x + 7\n",
            "    *   \"You get 31\": 2x + 7 = 31\n",
            "\n",
            "3.  **Solve the equation:**\n",
            "    *   Subtract 7 from both sides: 2x + 7 - 7 = 31 - 7\n",
            "    *   Simplify: 2x = 24\n",
            "    *   Divide both sides by 2: 2x / 2 = 24 / 2\n",
            "    *   Simplify: x = 12\n",
            "\n",
            "**Answer:** The number you are thinking of is 12.\n",
            "\n",
            "\n",
            "\n",
            "============================================================\n",
            "EXAMPLE 6: Project Planning\n",
            "============================================================\n",
            "\n",
            "============================================================\n",
            "🚀 Plan-and-Solve+ (Enhanced)\n",
            "============================================================\n",
            "Problem: You need to complete a project in 10 days. \n",
            "Task A takes 3 days, Task B takes 4 days, Task C takes 2 days. \n",
            "Task B depends on Task A being complete. Task C can run parallel to anything. \n",
            "Can you finish in time? What's the optimal schedule?\n",
            "\n",
            "📋 Step 1: Extracting relevant information...\n",
            "\n",
            "Extracted Info:\n",
            "Okay, let's break down this project scheduling problem.\n",
            "\n",
            "**Variables and Values:**\n",
            "\n",
            "*   **Total Project Deadline:** 10 days\n",
            "*   **Task A Duration:** 3 days\n",
            "*   **Task B Duration:** 4 days\n",
            "*   **Task C Duration:** 2 days\n",
            "*   **Dependency:** Task B depends on Task A being completed.\n",
            "*   **Parallel Execution:** Task C can be executed concurrently with other tasks.\n",
            "\n",
            "\n",
            "🗺️  Step 2: Generating high-level plan...\n",
            "\n",
            "Plan:\n",
            "Okay, here's a step-by-step plan to solve the project scheduling problem:\n",
            "\n",
            "1.  **Calculate the sequential duration of dependent tasks:**\n",
            "\n",
            "    *   Since Task B depends on Task A, we first add their durations: 3 days (Task A) + 4 days (Task B) = 7 days. This is the minimum time required to complete A and B sequentially.\n",
            "\n",
            "2.  **Factor in the parallel task (Task C):**\n",
            "\n",
            "    *   Task C can run concurrently with either Task A or Task B (or both partially). Since Task C only takes 2 days, and A+B take 7 days, Task C can easily fit in without extending the overall timeline.\n",
            "\n",
            "3.  **Determine the total project duration:**\n",
            "\n",
            "    *   Since Task C can run in parallel, the total project duration is determined by the sequential tasks A and B, which require 7 days. Task C doesn't add to this duration as it can happen at the same time as either A or B.\n",
            "\n",
            "4.  **Compare total duration with the deadline:**\n",
            "\n",
            "    *   The calculated total project duration is 7 days.  The project deadline is 10 days.\n",
            "\n",
            "5.  **Check if the project can be completed within the deadline:**\n",
            "\n",
            "    *   Since 7 days (project duration) is less than 10 days (deadline), the project can be completed within the given timeframe.\n",
            "\n",
            "6.  **Develop an optimal schedule:**\n",
            "\n",
            "    *   Here's one optimal schedule:\n",
            "\n",
            "        *   **Day 1-3:** Task A\n",
            "        *   **Day 1-2:** Task C (Runs parallel to Task A)\n",
            "        *   **Day 4-7:** Task B\n",
            "\n",
            "7.  **Final Answer:**\n",
            "\n",
            "    *   Yes, the project can be finished in time. The optimal schedule involves running Task C parallel to Task A, completing Task A in 3 days, and Task B in 4 days immediately after, for a total project duration of 7 days.\n",
            "\n",
            "\n",
            "⚡ Step 3: Executing plan with careful calculations...\n",
            "\n",
            "Execution:\n",
            "Okay, let's execute the plan step-by-step, showing all calculations and verifications.\n",
            "\n",
            "**Step 1: Calculate the sequential duration of dependent tasks:**\n",
            "\n",
            "*   Task A duration: 3 days\n",
            "*   Task B duration: 4 days\n",
            "*   Tasks A and B are dependent (B depends on A).\n",
            "*   Combined duration (A then B): 3 days + 4 days = 7 days\n",
            "\n",
            "**Verification:** This step correctly identifies the sequential nature of tasks A and B and accurately sums their durations.\n",
            "\n",
            "**Step 2: Factor in the parallel task (Task C):**\n",
            "\n",
            "*   Task C duration: 2 days\n",
            "*   Task C can run parallel to either Task A or Task B.\n",
            "*   Since the combined duration of A and B is 7 days, and Task C only takes 2 days, Task C can easily fit within the A+B timeline.\n",
            "\n",
            "**Verification:** This step correctly recognizes the flexibility of Task C and how its shorter duration allows it to run alongside A or B without delaying the overall project.\n",
            "\n",
            "**Step 3: Determine the total project duration:**\n",
            "\n",
            "*   Sequential tasks A and B: 7 days\n",
            "*   Task C runs in parallel and doesn't extend the critical path (A then B).\n",
            "*   Total project duration: 7 days\n",
            "\n",
            "**Verification:** This accurately identifies the critical path (A then B) and correctly determines the total project duration based on that path.  Task C doesn't add to the total time because it's done in parallel.\n",
            "\n",
            "**Step 4: Compare total duration with the deadline:**\n",
            "\n",
            "*   Total project duration: 7 days\n",
            "*   Project deadline: 10 days\n",
            "\n",
            "**Verification:** This step accurately states both values for comparison.\n",
            "\n",
            "**Step 5: Check if the project can be completed within the deadline:**\n",
            "\n",
            "*   7 days (project duration) < 10 days (deadline)\n",
            "*   Therefore, the project can be completed within the deadline.\n",
            "\n",
            "**Verification:** This is a correct conclusion based on the comparison in the previous step.\n",
            "\n",
            "**Step 6: Develop an optimal schedule:**\n",
            "\n",
            "*   **Option 1:**\n",
            "    *   Day 1-3: Task A\n",
            "    *   Day 1-2: Task C (Runs parallel to Task A)\n",
            "    *   Day 4-7: Task B\n",
            "\n",
            "*   **Option 2:**\n",
            "    *   Day 1-3: Task A\n",
            "    *   Day 4-5: Task C (Runs parallel to Task B)\n",
            "    *   Day 4-7: Task B\n",
            "\n",
            "**Verification:**  Both schedules are valid and optimal. They both ensure task dependencies are met (A before B) and that C runs in parallel.\n",
            "\n",
            "**Step 7: Final Answer:**\n",
            "\n",
            "*   Yes, the project can be finished in time.  The optimal schedule involves running Task C parallel to Task A or B, completing Task A in 3 days, and Task B in 4 days immediately after, for a total project duration of 7 days.\n",
            "\n",
            "**Overall Verification:**\n",
            "\n",
            "The entire plan is logically sound, the calculations are accurate, and the conclusions are valid. The identified schedule is indeed optimal, as it completes the project within the shortest possible time while respecting all dependencies.\n",
            "\n",
            "\n",
            "\n",
            "============================================================\n",
            "EXAMPLE 7: Method Comparison\n",
            "============================================================\n",
            "\n",
            "======================================================================\n",
            "🔬 COMPARING METHODS\n",
            "======================================================================\n",
            "\n",
            "\n",
            "============================================================\n",
            "📝 Basic CoT (Baseline)\n",
            "============================================================\n",
            "Problem: Sarah has 3 boxes of cookies. Each box has 12 cookies. \n",
            "She gives 8 cookies to her friend and eats 5 herself. \n",
            "How many cookies does she have left?\n",
            "\n",
            "Response:\n",
            "1. **Total cookies:** Sarah starts with 3 boxes * 12 cookies/box = 36 cookies.\n",
            "2. **Cookies given away:** She gives away 8 cookies.\n",
            "3. **Cookies eaten:** She eats 5 cookies.\n",
            "4. **Total cookies removed:** She removed a total of 8 + 5 = 13 cookies.\n",
            "5. **Cookies remaining:** Sarah has 36 - 13 = 23 cookies left.\n",
            "\n",
            "**Answer:** Sarah has 23 cookies left.\n",
            "\n",
            "\n",
            "\n",
            "============================================================\n",
            "🎯 Plan-and-Solve\n",
            "============================================================\n",
            "Problem: Sarah has 3 boxes of cookies. Each box has 12 cookies. \n",
            "She gives 8 cookies to her friend and eats 5 herself. \n",
            "How many cookies does she have left?\n",
            "\n",
            "Response:\n",
            "Plan:\n",
            "\n",
            "1.  Find the total number of cookies Sarah starts with.\n",
            "2.  Find the total number of cookies Sarah gives away and eats.\n",
            "3.  Subtract the total number of cookies given away and eaten from the total number of cookies Sarah started with.\n",
            "\n",
            "Step 1: Find the total number of cookies Sarah starts with.\n",
            "Sarah has 3 boxes of cookies, and each box has 12 cookies.\n",
            "Total cookies = 3 boxes * 12 cookies/box = 36 cookies\n",
            "\n",
            "Step 2: Find the total number of cookies Sarah gives away and eats.\n",
            "Sarah gives away 8 cookies and eats 5 cookies.\n",
            "Total cookies given away and eaten = 8 cookies + 5 cookies = 13 cookies\n",
            "\n",
            "Step 3: Subtract the total number of cookies given away and eaten from the total number of cookies Sarah started with.\n",
            "Cookies left = Total cookies - (Cookies given away and eaten)\n",
            "Cookies left = 36 cookies - 13 cookies = 23 cookies\n",
            "\n",
            "Final Answer: Sarah has 23 cookies left.\n",
            "\n",
            "\n",
            "\n",
            "============================================================\n",
            "🚀 Plan-and-Solve+ (Enhanced)\n",
            "============================================================\n",
            "Problem: Sarah has 3 boxes of cookies. Each box has 12 cookies. \n",
            "She gives 8 cookies to her friend and eats 5 herself. \n",
            "How many cookies does she have left?\n",
            "\n",
            "📋 Step 1: Extracting relevant information...\n",
            "\n",
            "Extracted Info:\n",
            "Here are the variables and their values from the word problem:\n",
            "\n",
            "*   **Number of boxes of cookies:** 3\n",
            "*   **Number of cookies per box:** 12\n",
            "*   **Number of cookies given to friend:** 8\n",
            "*   **Number of cookies Sarah eats:** 5\n",
            "\n",
            "\n",
            "🗺️  Step 2: Generating high-level plan...\n",
            "\n",
            "Plan:\n",
            "Here's a step-by-step plan to solve the problem:\n",
            "\n",
            "1.  **Calculate the total number of cookies:** Multiply the number of boxes by the number of cookies per box.  (3 boxes * 12 cookies/box)\n",
            "\n",
            "2.  **Calculate the total number of cookies Sarah gives away or eats:** Add the number of cookies given to her friend and the number of cookies she eats. (8 cookies + 5 cookies)\n",
            "\n",
            "3.  **Calculate the number of cookies remaining:** Subtract the total number of cookies given away or eaten from the total number of cookies Sarah started with. (Total cookies - Cookies given away/eaten)\n",
            "\n",
            "\n",
            "⚡ Step 3: Executing plan with careful calculations...\n",
            "\n",
            "Execution:\n",
            "Okay, let's follow the plan step-by-step:\n",
            "\n",
            "**Step 1: Calculate the total number of cookies**\n",
            "\n",
            "*   Sarah has 3 boxes of cookies and each box contains 12 cookies.\n",
            "*   Total cookies = 3 boxes * 12 cookies/box = 36 cookies\n",
            "\n",
            "**Verification:** This calculation is straightforward multiplication.\n",
            "\n",
            "**Step 2: Calculate the total number of cookies Sarah gives away or eats**\n",
            "\n",
            "*   Sarah gives 8 cookies to her friend and eats 5 cookies herself.\n",
            "*   Total cookies given away/eaten = 8 cookies + 5 cookies = 13 cookies\n",
            "\n",
            "**Verification:** Simple addition.\n",
            "\n",
            "**Step 3: Calculate the number of cookies remaining**\n",
            "\n",
            "*   Sarah started with 36 cookies and gave away/ate 13 cookies.\n",
            "*   Remaining cookies = 36 cookies - 13 cookies = 23 cookies\n",
            "\n",
            "**Verification:** Subtraction is correct.\n",
            "\n",
            "**Answer:** Sarah has 23 cookies left.\n",
            "\n",
            "\n",
            "======================================================================\n",
            "📊 SUMMARY\n",
            "======================================================================\n",
            "All three methods completed. Review outputs above.\n",
            "\n",
            "\n",
            "============================================================\n",
            "EXAMPLE 8: Algorithm Design\n",
            "============================================================\n",
            "\n",
            "============================================================\n",
            "🎯 Plan-and-Solve\n",
            "============================================================\n",
            "Problem: Design an algorithm to check if a string is a palindrome. \n",
            "What are the steps? What edge cases should be considered?\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "yw5cGeG1UB5G"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}